package cn.frh.org.service.impl;

import cn.frh.basic.service.impl.BaseServiceImpl;
import cn.frh.log.annotation.Log;
import cn.frh.org.domain.Department;
import cn.frh.org.mapper.DepartmentMapper;
import cn.frh.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {
    @Autowired
    private DepartmentMapper departmentMapper;
    @Override
    @Transactional
    @Log(value = "添加部门")
    public void add(Department department) {
        departmentMapper.save(department);
        this.update(department);
    }

    @Override
    @Transactional
    @Log(value = "修改部门")
    public void update(Department department) {
        String dirPath = "";
        if (department.getParent() == null){
            //如果是顶级部门
            dirPath = "/"+ department.getId();//需要返回自增id
        }else {
            //如果不是顶级部门
            Department parentDept = departmentMapper.loadById(department.getParent().getId());
            //用父的dirpath加上自己的id
            dirPath = parentDept.getDirPath() + "/" + department.getId();
        }
        department.setDirPath(dirPath);
        departmentMapper.update(department);
    }


    @Override
    @Log(value = "查询部门树")
    public List<Department> findOneAndTwoLevelTree() {
        return departmentMapper.findOneAndTwoLevelTree();
    }
}
